package cn.cheqi.recovery.background.controller;


import cn.cheqi.recovery.background.dto.DataGridView;
import cn.cheqi.recovery.background.dto.ResultDTO;
import cn.cheqi.recovery.background.entity.Building;
import cn.cheqi.recovery.background.service.BuildingService;
import cn.cheqi.recovery.background.vo.BuildingVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  楼栋前端控制器
 * </p>
 *
 * @author Robin
 * @since 2020-02-23
 */
@RestController
@RequestMapping("/system/building")
public class BuildingController {

    @Autowired
    private BuildingService buildingService;

    /**
     * 查出所有楼栋信息
     * @param buildingVO
     * @return
     */
    @RequestMapping("/loadAllBuildings")
    public DataGridView loadAllBuildings(BuildingVO buildingVO){
        IPage<Building> page = new Page<>(buildingVO.getPage(),buildingVO.getLimit());
        QueryWrapper<Building> wrapper = new QueryWrapper<>();
        //获取模糊查询的条件
        //楼栋编号
        Long buildingId = buildingVO.getBuildingId();
        //小区名称
        String communityName = buildingVO.getCommunityName();
        //详细地址
        String detailAddress = buildingVO.getDetailAddress();
        if (buildingId != null){
            wrapper.like("building_id",buildingId);
        }
        if (!StringUtils.isBlank(communityName)){
            String newInfo = communityName.replace(" ", "");
            wrapper.like(!StringUtils.isBlank(newInfo),"community_name",newInfo);
        }
        if (detailAddress != null){
            wrapper.like("detail_address",detailAddress);
        }
        wrapper.orderByDesc("building_id");
        buildingService.page(page,wrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }


    /**
     * 添加楼栋信息
     * @param buildingVO
     * @return
     */
    @PostMapping("/addBuilding")
    @Transactional
    public ResultDTO addBuildingInfo(BuildingVO buildingVO){
        try {
            buildingService.save(buildingVO);
            return new ResultDTO(200,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO(-1,"添加失败");
        }
    }

    /**
     * 根据楼栋ID修改楼栋信息
     * @param buildingVO
     * @return
     */
    @PostMapping("/editBuilding")
    @Transactional
    public ResultDTO editBuildingInfoById(BuildingVO buildingVO){
        try {
            buildingService.updateById(buildingVO);
            return new ResultDTO(200,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO(-1,"修改失败");
        }
    }

    /**
     * 通过楼栋ID删除楼栋信息
     * @param buildingVO
     * @return
     */
    @PostMapping("/deleteBuildingById")
    @Transactional
    public ResultDTO deleteBuildingById(BuildingVO buildingVO){
        try {
            buildingService.removeById(buildingVO.getBuildingId());
            return new ResultDTO(200,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO(-1,"删除失败");
        }
    }
}

